package com.twg.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter{
	String encoding="utf-8";
	public void destroy() {
		System.out.println("销毁");
		
	}
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("过滤字符集.");
		req.setCharacterEncoding(encoding);
		resp.setCharacterEncoding(encoding);
		resp.setContentType("text/html;charset="+encoding);
		chain.doFilter(req,resp);
	}

	public void init(FilterConfig arg0) throws ServletException {
		encoding=arg0.getInitParameter("encoding");
		System.out.println("初始化filter,字符集"+encoding);
	}

}
